import { reqGoodsInfo,reqAddupDateShopCart } from "@/api";
// 封装游客身份模块uuid---》生成一个随机字符串（但不能在变）
import { getUUID } from "@/utils/uuid_token";

const actions = {
    async goodsInfo({commit},skyid) {
       let result = await reqGoodsInfo(skyid)
       if(result.code == 200) {
            commit('GOODSINFO',result.data)
       }
    },
    // 获取商品的id和购买的数量   带两参数要写对象{}
    async upDateShopCart({commit},{skuId,skuNum}) {
        let result = await reqAddupDateShopCart(skuId,skuNum) 
            if(result.code == 200) {
                return 'ok'
            } else {
                return Promise.reject()
            }
       }
}

const mutations = {
    GOODSINFO(state,goodsInfo) {
        state.goodsInfo = goodsInfo
    }
}

const state = {
    goodsInfo:{},
    // 游客的临时身份
    unid_token:getUUID()
}

const getters = {
    // 简化skuInfo
    skuInfo(state) {
        return state.goodsInfo.skuInfo || {}
    },
    spuSaleAttrList(state) {
        return state.goodsInfo.spuSaleAttrList || {}
    },
    categoryView(state) {
        return state.goodsInfo.categoryView
    }
}

export default {
    actions,
    mutations,
    state,
    getters
}